package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONValue;

import com.google.gson.Gson;

public class DiagnosticTestRecordServlet extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String ADD_DIAGNOSTIC_TEST_RECORD = "add-diagnostic-test-record";
	private static final String OPEN_RECORD = "open-record";

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	private void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String mode = request.getParameter("mode");
		Map<String, Object> data = new HashMap<String, Object>();
		if (mode.equals(DiagnosticTestRecordServlet.ADD_DIAGNOSTIC_TEST_RECORD)) {
			Integer status = Integer.parseInt(""
					+ request.getParameter("status"));
			Integer patientId = Integer.parseInt(""
					+ request.getParameter("patientId"));
			String labTestIdList = request.getParameter("labTestIdList");

			JSONArray arr = (JSONArray) JSONValue.parse(labTestIdList);
			LabTestRecord r = PatientManager.getInstance().createLabTestRecord(
					new Date(), status, 1, patientId, computeTotalPrice(arr));

			if (r != null) {
				Integer labTestRecordId = r.getLabTestRecord();
				Iterator i = arr.iterator();

				while (i.hasNext()) {
					Integer labTestId = Integer.parseInt("" + i.next());

					PatientManager.getInstance().createLabTestList(
							labTestRecordId, labTestId);
				}
			}

			data.put("id", r.getLabTestRecord());
			data.put("date", r.getDate());
			data.put("staff", r.getStaffId());
		} else if (mode.equals(OPEN_RECORD)) {
			Integer labTestRecordId = Integer.parseInt(""
					+ request.getParameter("labTestRecordId"));

			LabTestRecord l = LabTestManager.getInstance().getLabTestRecord(
					labTestRecordId);
			ArrayList<LabTestList> arr = LabTestManager.getInstance()
					.getLabTestListOfLabTestRecord(labTestRecordId);

			data.put("id", l.getLabTestRecord());
			data.put("status", getStatus(l.getStatus()));
			data.put("totalPrice", l.getTotalPrice());
			data.put("staff", l.getStaffId());
			data.put("date", l.getDate());
			data.put("labTests", toJSON(arr));
		}

		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(new Gson().toJson(data));
	}

	private String getStatus(Integer status) {
		switch (status) {
		case 1:
			return "In Queue";
		case 2:
			return "On Going";
		case 3:
			return "Paid";
		case 4:
			return "Processed";
		}
		return "Unknown";
	}

	private ArrayList<Map<String, Object>> toJSON(ArrayList<LabTestList> arr) {
		ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		for (LabTestList l : arr) {
			Map<String, Object> obj = new LinkedHashMap<String, Object>();

			LabTest t = LabTestManager.getInstance().getLabTest(
					l.getLabTestId());
			LabTestCategory c = LabTestManager.getInstance()
					.getLabTestCategory(t.getCategoryId());

			obj.put("name", t.getName());
			obj.put("price", t.getPrice());
			obj.put("category", c.getCategoryName());
			
			list.add(obj);
		}

		if (list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	private Float computeTotalPrice(JSONArray arr) {
		Iterator i = arr.iterator();
		Float f = 0f;
		while (i.hasNext()) {
			Integer labTestId = Integer.parseInt("" + i.next());
			LabTestDAO dao = new LabTestDAO();
			LabTest l = dao.getLabTest(labTestId);

			f += l.getPrice();
		}
		return f;
	}

}
